package com.woniu.supermarket.web;

import com.alibaba.druid.wall.Violation;
import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.StockTakingCheck;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IStockTakingService;
import com.woniu.supermarket.util.CodeCreateUtil;
import com.woniu.supermarket.vo.StockTakingVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("StockTaking")
@Api(value = "库存盘点操作")
public class StockTakingController {

    @Autowired
    IStockTakingService iStockTakingService;

    @RequestMapping("/showList")
    public ResultResponse<PageInfo<StockTakingVo>> showList(@RequestParam(name = "now",defaultValue = "1") Integer now,
                                             @RequestParam(name = "size",defaultValue = "3") Integer size,
                                             @RequestParam(name = "status") Integer status,
                                             String code){
        ResultResponse<PageInfo<StockTakingVo>> result = null;
        try {
            PageInfo<StockTakingVo> lists = iStockTakingService.findByStockId(now, size,code,status);
            result = new ResultResponse<>(200,"查询库存盘点成功",lists);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询坤村盘点失败");
        }
        return result;
    }


    @DeleteMapping("/del/{id}")
    public ResultResponse<Void> del(@PathVariable("id")Integer id){
        ResultResponse<Void> result = null;
        try {
            iStockTakingService.removeStockTakingById(id);
            result = new ResultResponse<>(200,"删除库存盘点成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除库存盘点失败");
        }
        return result;
    }



    @GetMapping("/findGoods/{sid}")
    public ResultResponse<List<StockTakingVo>> findGoods(@PathVariable("sid")Integer sid){
        ResultResponse<List<StockTakingVo>> result = null;
        try {
            List<StockTakingVo> lists = iStockTakingService.findGoodsByStockId(sid);
            result = new ResultResponse<>(200,"查询库存盘点详情成功",lists);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询库存盘点详情失败");
        }
        return result;
    }



    @PostMapping("/auditStockTaking")
    public ResultResponse<Void> auditStockTaking(@RequestBody StockTakingCheck stockTakingCheck){
        ResultResponse<Void> result = null;
        try {
            iStockTakingService.addStockTakingCheck(stockTakingCheck);
            result = new ResultResponse<>(200,"添加库存盘点审核成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加库存盘点审核失败");
        }
        return result;
    }


    @GetMapping("/getCode")
    public ResultResponse<String> getCode(){
        ResultResponse<String> result = null;
        try {
            String code = CodeCreateUtil.createStockTakingCode();
            result = new ResultResponse<>(200,"获取库存盘点单号成功",code);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"获取库存盘点单号失败");
        }
        return result;
    }

    @RequestMapping("/addStockTaking")
    public ResultResponse<Void> addStockTaking(@RequestBody StockTakingVo stockTakingVo){
        ResultResponse<Void> result = null;
        try {
            iStockTakingService.addStockTaking(stockTakingVo);
            result = new ResultResponse<>(200,"添加库存盘点失败");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加库存盘点失败");
        }
        return result;
    }
}
